package com.thinkingdata.datax.plugin.writer.util;


import com.alibaba.datax.common.element.Column;
import com.alibaba.datax.common.exception.DataXException;
import com.thinkingdata.datax.plugin.writer.exption.TaDataWriterErrorCode;
import com.thinkingdata.datax.plugin.writer.meta.TaDataWriterColumn;
import org.apache.commons.lang3.StringUtils;

public class ColumnAnalysis {


    private enum Type {
        STRING, NUMBER, DATE, BOOLEAN
    }


    /**
     * 指定类型转换，如果有指定类型，优先转换为配置文件中的类型
     *
     * @Author: Felix.Wang
     * @Date: 2019/8/13 11:19
     */
    public static  Object castValueToAppointValue(TaDataWriterColumn taDataWriterColumn, Column column) {

        Type type = null;

        if (StringUtils.isBlank(taDataWriterColumn.getType())) {
            switch (column.getType()) {
                case LONG:
                    type = Type.NUMBER;
                    break;
                case BOOL:
                    type = Type.BOOLEAN;
                    break;
                case DATE:
                    type = Type.DATE;
                    break;
                case BYTES:
                    type = Type.STRING;
                    break;
                case STRING:
                    type = Type.STRING;
                    break;
                case DOUBLE:
                    type = Type.NUMBER;
                    break;
                case INT:
                    type = Type.NUMBER;
                    break;
            }
        } else {
            type = Type.valueOf(taDataWriterColumn.getType().toUpperCase());
        }


        switch (type) {
            case STRING:
                return column.asString();
            case NUMBER:
                return Double.valueOf(column.asString());
            case BOOLEAN:
                return Boolean.valueOf(column.asString());
            case DATE:
                return DateUtil.formatDateByFormatStr(column.asDate(), taDataWriterColumn.getDateFormat());
            default:
                String errorMessage = String.format(
                        "您配置的列类型暂不支持 : [%s]", taDataWriterColumn.getType());
                throw DataXException
                        .asDataXException(
                                TaDataWriterErrorCode.NOT_SUPPORT_TYPE,
                                errorMessage);
        }

    }
    /**
     * 指定类型转换
     *
     * @Author: Felix.Wang
     * @Date: 2019/8/13 11:19
     */
    public static  Object castValueToAppointValue(String columType, String value) {

        Type type = Type.valueOf(columType.toUpperCase());

        switch (type) {
            case STRING:
                return value;
            case NUMBER:
                return Double.valueOf(value);
            case BOOLEAN:
                return Boolean.valueOf(value);
            case DATE:
                return DateUtil.formatMsPatterndate(DateUtil.parseDateString(value));
            default:
                String errorMessage = String.format(
                        "您配置的列类型暂不支持 : [%s]", columType);

                throw DataXException
                        .asDataXException(
                                TaDataWriterErrorCode.NOT_SUPPORT_TYPE,
                                errorMessage);
        }

    }


}

